package cn.cdeden.resource.unified.service;


import cn.cdeden.common.oss.enumd.OssModuleOperate;
import cn.cdeden.common.oss.enumd.OssModuleType;
import cn.cdeden.resource.api.OssUploadService;
import cn.cdeden.resource.api.domain.*;
import com.alibaba.fastjson2.JSONObject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.dubbo.rpc.*;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


/**
 * 实现
 * @ProjectName : KnowCore01
 * @Package : cn.cdeden.resource.unified.service
 * @ClassName : RemoteCallLogFilter
 * @Author : Yu.
 * @Description : // TODO
 * @Date : 2024/12/4 22:42
 * @Version : 1.0
 */
@Component
@Slf4j
@Validated
@RequiredArgsConstructor
public class RemoteUploadFileFilter {

    /**
     * @param moduleId 在模块里的编号
     * @param userId   要查看的用户会
     * @param info     其他参数信息
     * @param operate
     * @return cn.cdeden.resource.api.domain.RemoteFile
     * @author Yu.
     * @Description // 获取文件信息
     * @Date 2024/12/8 14:28
     **/

    public RemoteFile getFileDataInfo(String moduleId, Long userId , JSONObject info , OssModuleOperate operate,String type){
        return gssUploadService(type).getFileDataInfo(moduleId,userId,info,operate);
    }



    /**
     * @author Yu.
     * @Description // 到OSS 前验证 参数是否正确  放回基本文件储存信息   有问题直接抛出异常
     * @Date 2024/12/4 17:44
     * @param info 验证参数
     * @param userId 操作者
     * @return String  放回的数据 有值就是 提示保存
     **/
    public RemoteOssUpload verifyData(RemoteOssUpload info, Long userId,String type){
        return gssUploadService(type).verifyData(info,userId);
    }


    /**
     * 上传文件
     *
     * @param upload
     * @param userId
     * @return java.lang.Object
     * @author Yu.
     * @Date 2024/12/5 13:53
     **/
    public RemoteFilesBucketPath upload(RemoteFilesUpload upload, Long userId,String type){
        return gssUploadService(type).upload(upload,userId);
    }



    /**
     * 下载文件夹
     * @author Yu.
     * @Date 2024/12/5 18:21
     * @param idList
     * @param userId
     * @param jsonObject
     * @return java.util.List<cn.cdeden.resource.api.domain.RemoteFilesDownload>
     **/
    public List<RemoteFilesDownload> downloadFile(List<String> idList, Long userId , JSONObject jsonObject,String type){
        return gssUploadService(type).downloadFile(idList,userId,jsonObject);
    }




    /*****************************************文件的工厂处理************************************************************/


    @DubboReference(check = false, group = "temporary", version = "1.0.0", timeout = 50000)
    private final OssUploadService temporary;

    @DubboReference(check = false, group = "enterprise", version = "1.0.0", timeout = 50000)
    private final OssUploadService enterprise;

    @DubboReference(check = false, group = "personal", version = "1.0.0", timeout = 50000)
    private final OssUploadService personal;

    @DubboReference(check = false, group = "template", version = "1.0.0", timeout = 50000)
    private final OssUploadService template;

    @DubboReference(check = false, group = "dify", version = "1.0.0", timeout = 50000)
    private final OssUploadService dify;

    @DubboReference(check = false, group = "team", version = "1.0.0", timeout = 50000)
    private final OssUploadService team;

    @DubboReference(check = false, group = "teamTemplate", version = "1.0.0", timeout = 50000)
    private final OssUploadService teamTemplate;

    @DubboReference(check = false, group = "publicImages", version = "1.0.0", timeout = 50000)
    private final OssUploadService publicImages;

    @DubboReference(check = false, group = "qa", version = "1.0.0", timeout = 50000)
    private final OssUploadService qa;

    @DubboReference(check = false, group = "share", version = "1.0.0", timeout = 50000)
    private final OssUploadService share;

    /**
     * @param type
     * @return cn.cdeden.resource.api.OssUploadService
     * @author Yu.
     * @Description // 获取文件上传的实现 处理
     * @Date 2024/12/5 11:21
     **/
    public OssUploadService gssUploadService(String type) {
        OssModuleType coedInfo = OssModuleType.findCoedInfo(type);
        return switch (coedInfo) {
            case Temporary -> temporary; //// 临时模块
            case public_images -> publicImages; ////图片文件
//            case Team -> temporary; //// 团队模块
            case Enterprise -> enterprise; //// 企业模块
            case Personal -> personal; //// 个人模块
            case Template -> template;////文件模版模块
            case Dify -> dify;
            case Qa -> qa; //个人文档问答上传附件
            case Team -> team;//// 团队模块
            case TeamTemplate -> teamTemplate;
            case Share -> share;
            default -> throw new IllegalStateException("错误的文件上传参数: " + type);
        };
    }






}
